//
//  JPProgressHUD.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/5/26.
//

#import "JPProgressHUD.h"

@implementation JPProgressHUD

+ (instancetype)showLoading
{
    UIWindow *keyWindow = KEY_WINDOW;
    return [self showLoadingInView:keyWindow];
}
+ (instancetype)showLoadingInView:(UIView *)view
{
    if (view == nil) return nil;

    return  [JPProgressHUD showHUDAddedTo:view animated:YES];
}

+ (instancetype)showRightMsg:(NSString *)msg
{
    UIWindow *keyWindow = KEY_WINDOW;
    return [self showRightMsg:msg inView:keyWindow];
}
+ (instancetype)showRightMsg:(NSString *)msg inView:(UIView *)view
{
    NSString *l_msg = @"";
    if ([msg isKindOfClass:[NSString class]]) {
        l_msg = msg;
    }
      return [self __JP_showHUDWithImage:[UIImage imageNamed:@"jp_hub_right"] msg:l_msg inView:view];
}

+ (instancetype)showWrongMsg:(NSString *)msg
{
    UIWindow *keyWindow = KEY_WINDOW;
    return [self showWrongMsg:msg inView:keyWindow];
}
+ (instancetype)showWrongMsg:(NSString *)msg inView:(UIView *)view{
   
    NSString *l_msg = @"";
    if ([msg isKindOfClass:[NSString class]]) {
        l_msg = msg;
    }
    return [self __JP_showHUDWithImage:[UIImage imageNamed:@"jp_hub_error"] msg:l_msg inView:view];
}

+ (instancetype)showMsg:(NSString *)msg
{
    UIWindow *keyWindow = KEY_WINDOW;
    return [self showMsg:msg inView:keyWindow];
}
+ (instancetype)showMsg:(NSString *)msg inView:(UIView *)view{
   
    NSString *l_msg = @"";
    if ([msg isKindOfClass:[NSString class]]) {
        l_msg = msg;
    }
    return [self __JP_showHUDWithMsg:l_msg inView:view];
}

+ (void)hideLoading
{
    UIWindow *keyWindow = KEY_WINDOW;
    [JPProgressHUD hideHUDForView:keyWindow animated:YES];
}


/// 私有
+ (instancetype)__JP_showHUDWithImage:(UIImage *)image msg:(NSString *)msg inView:(UIView *)view
{
    if (view == nil) return nil;
    
    JPProgressHUD *hud = [JPProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    UIImage *_image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    hud.customView = [[UIImageView alloc] initWithImage:_image];
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.0f];
    hud.bezelView.color = UIColorRGBAlpha(0, 0, 0, 1.0);
    hud.bezelView.blurEffectStyle = UIBlurEffectStyleLight;
    hud.bezelView.layer.cornerRadius = 10.0;
    hud.label.numberOfLines = 0;
    hud.label.text = msg;
    hud.label.textColor = [UIColor whiteColor];
    hud.label.font = JPUIFontPingFangSCRegular(16);
    [hud hideAnimated:YES afterDelay:0.5];
    return hud;
}

+ (instancetype)__JP_showHUDWithMsg:(NSString *)msg inView:(UIView *)view{
    if (view == nil) return nil;
    
    JPProgressHUD *hud = [JPProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.0f];
    hud.bezelView.color = UIColorRGBAlpha(0, 0, 0, 1.0);
    hud.bezelView.blurEffectStyle = UIBlurEffectStyleLight;
    hud.bezelView.layer.cornerRadius = 10.0;
    hud.label.numberOfLines = 0;
    hud.label.text = msg;
    hud.label.textColor = [UIColor whiteColor];
    hud.label.font = JPUIFontPingFangSCRegular(16);
    [hud hideAnimated:YES afterDelay:0.5];
    return hud;
}

#pragma mark - 不自动消失的view
+ (instancetype)JPShowHUDWithMsg:(NSString *)msg inView:(UIView *)view{
    if (view == nil) return nil;
    JPProgressHUD *hud = [JPProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.backgroundView.color =[UIColor colorWithWhite:0.f alpha:0.0f];
    hud.bezelView.color = UIColorRGBAlpha(0, 0, 0, 1.0);
    hud.bezelView.blurEffectStyle = UIBlurEffectStyleLight;
    hud.bezelView.layer.cornerRadius = 10.0;
    hud.label.numberOfLines = 0;
    hud.label.text = msg;
    hud.label.textColor = [UIColor whiteColor];
    hud.label.font = JPUIFontPingFangSCRegular(16);
    return hud;
}

@end
